package converter;

public class DependencyWalker {
	
	public Map walkEffectivePom(File pom) {
		def root = new XmlSlurper().parse(pom)
		return getDependencies(root)
	}
	
	
	private Map buildDependeciesScopeMap() {
		Map dependeciesScopeMap = [:]
		DependencyScope.values().each {
			dependeciesScopeMap[it] = [] 
		}
		return dependeciesScopeMap
	 }
	
	private DependencyScope findDependecyScope(def elem){
		String scope = (elementHasText(elem.scope)) ? elem.scope : DependencyScope.compile
		return DependencyScope.valueOf(scope)
	}
	
	/**
	 * Check to see if the selected node has content
	 */
	private boolean elementHasText(it) {
	  return it.text().length() != 0
	}

	
	private Map getDependencies(project) {
		  Map dependencieMap = buildDependeciesScopeMap()
		  def dependencies = project.dependencies.dependency
		  //cleanup duplicates from parent
		  dependencies.each() {
			//if (!duplicateDependency(it, project, allProjects)) {
			  DependencyScope scope = findDependecyScope(it)
			  dependencieMap[scope] += it
		  }		  
		  return dependencieMap
	}
	
	
}
